//package com.yyplatform.admin.domain.job.quartz;
//
//import java.util.Objects;
//
//import org.quartz.CronScheduleBuilder;
//import org.quartz.CronTrigger;
//import org.quartz.JobBuilder;
//import org.quartz.JobDataMap;
//import org.quartz.JobDetail;
//import org.quartz.JobKey;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.TriggerBuilder;
//import org.quartz.TriggerKey;
//
//import com.yyplatform.admin.domain.job.entity.JobEntity;
//import com.yyplatform.admin.domain.job.enums.JobStatusEnum;
//import com.yyplatform.admin.infra.exception.ErrorCode;
//import com.yyplatform.admin.infra.exception.ServiceException;
//
//import lombok.extern.slf4j.Slf4j;
//
///**
// * Created on 2024-11-15
// */
//@Slf4j
//public class QuartzUtils {
//    /**
//     * 任务调度参数key
//     */
//    public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY";
//    private final static String JOB_NAME = "TASK_";
//
//    /**
//     * 获取触发器key
//     */
//    public static TriggerKey getTriggerKey(Long jobId) {
//        return TriggerKey.triggerKey(JOB_NAME + jobId);
//    }
//
//    /**
//     * 获取jobKey
//     */
//    public static JobKey getJobKey(Long jobId) {
//        return JobKey.jobKey(JOB_NAME + jobId);
//    }
//
//    /**
//     * 获取表达式触发器
//     */
//    public static CronTrigger getCronTrigger(Scheduler scheduler, Long jobId) {
//        try {
//            return (CronTrigger) scheduler.getTrigger(getTriggerKey(jobId));
//        } catch (SchedulerException e) {
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "get cron trigger error");
//        }
//    }
//
//    /**
//     * 创建定时任务
//     */
//    public static void createScheduleJob(Scheduler scheduler, JobEntity jobEntity) {
//        try {
//            //构建job信息
//            JobDetail jobDetail =
//                    JobBuilder.newJob(QuartzProxyJob.class).withIdentity(getJobKey(jobEntity.getId())).build();
//
//            //表达式调度构建器
//            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobEntity.getCronExpression())
//                    .withMisfireHandlingInstructionDoNothing();
//
//            //按新的cronExpression表达式构建一个新的trigger
//            CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobEntity.getId()))
//                    .withSchedule(scheduleBuilder).build();
//
//            //放入参数，运行时的方法可以获取
//            jobDetail.getJobDataMap().put(JOB_PARAM_KEY, jobEntity);
//
//            scheduler.scheduleJob(jobDetail, trigger);
//
//            //暂停任务
//            if (jobEntity.getStatus().equals(JobStatusEnum.PAUSE.getValue())) {
//                pauseJob(scheduler, jobEntity.getId());
//            }
//        } catch (SchedulerException e) {
//            log.error("create scheduler job error job info=[{}]", jobEntity, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "create schedule job error");
//        }
//    }
//
//    /**
//     * 更新定时任务
//     */
//    public static void updateScheduleJob(Scheduler scheduler, JobEntity jobEntity) {
//        try {
//            TriggerKey triggerKey = getTriggerKey(jobEntity.getId());
//
//            //表达式调度构建器
//            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(jobEntity.getCronExpression())
//                    .withMisfireHandlingInstructionDoNothing();
//
//            CronTrigger trigger = getCronTrigger(scheduler, jobEntity.getId());
//
//            //按新的cronExpression表达式重新构建trigger
//            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
//            //参数
//            trigger.getJobDataMap().put(JOB_PARAM_KEY, jobEntity);
//
//            scheduler.rescheduleJob(triggerKey, trigger);
//
//            //暂停任务
//            if (Objects.equals(jobEntity.getStatus(), JobStatusEnum.PAUSE.getValue())) {
//                pauseJob(scheduler, jobEntity.getId());
//            }
//
//        } catch (SchedulerException e) {
//            log.error("update scheduler job error jobInfo=[{}]", jobEntity, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "update schedule job error");
//        }
//    }
//
//    /**
//     * 立即执行任务
//     */
//    public static void run(Scheduler scheduler, JobEntity jobEntity) {
//        try {
//            //参数
//            JobDataMap dataMap = new JobDataMap();
//            dataMap.put(JOB_PARAM_KEY, jobEntity);
//
//            scheduler.triggerJob(getJobKey(jobEntity.getId()), dataMap);
//        } catch (SchedulerException e) {
//            log.error("run scheduler job error, jobInfo=[{}]", jobEntity, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "run job error");
//        }
//    }
//
//    /**
//     * 暂停任务
//     */
//    public static void pauseJob(Scheduler scheduler, Long jobId) {
//        try {
//            scheduler.pauseJob(getJobKey(jobId));
//        } catch (SchedulerException e) {
//            log.error("pause scheduler error jobId=[{}]", jobId, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "pause job error");
//        }
//    }
//
//    /**
//     * 恢复任务
//     */
//    public static void resumeJob(Scheduler scheduler, Long jobId) {
//        try {
//            scheduler.resumeJob(getJobKey(jobId));
//        } catch (SchedulerException e) {
//            log.error("resume scheduler error jobId=[{}]", jobId, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "resume job error");
//        }
//    }
//
//    /**
//     * 删除定时任务
//     */
//    public static void deleteScheduleJob(Scheduler scheduler, Long jobId) {
//        try {
//            scheduler.deleteJob(getJobKey(jobId));
//        } catch (SchedulerException e) {
//            log.error("delete scheduler error jobId=[{}]", jobId, e);
//            throw ServiceException.of(ErrorCode.JOB_TRIGGER_ERROR, "delete job error");
//        }
//    }
//
//}
